package cn.edu.jxau.test;

/**
 * 迭代实现斐波那契数列
 * 
 * @author Fudashi
 */
public class Main {

	public static void main(String[] args) {
		// 0 1 1 2 3 5 8 13 21
		System.out.println(fibonacci(5));
		System.out.println(fibonacci(6));
		System.out.println(fibonacci(7));
		System.out.println(fibonacci(8));
	}

	private static long fibonacci(long n) {

		if (n == 0) {
			return 0;
		} else if (n == 1) {
			return 1;
		} else {
			long pre = 0;
			long next = 1;
			long result = 0;
			for (int i = 2; i < n; i++) {
				result = pre + next;
				pre = next;
				next = result;
			}
			return pre + next;
		}
	}
}
